﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Nereus.NPOI
{
    using Attributes;
    using global::NPOI.OpenXmlFormats.Wordprocessing;
    using global::NPOI.XWPF.UserModel;
    using Power;
    using System.IO;
    using System.Linq;

    public class NPOIBuilder : INDoucmentBuilder
    {
        public INDocumentInfo Build(IEnumerable<INClassContent> nClassContents)
        {
            var _nDocumentInfo = new NDocumentInfo();
            _nDocumentInfo.Type = EnumDocumentType.FilePath;
            _nDocumentInfo.Name = "API接口文档.doc";
            _nDocumentInfo.Path = "./";
            _nDocumentInfo.FileStream = new FileStream(_nDocumentInfo.Path + _nDocumentInfo.Name, FileMode.Create);
            
            XWPFDocument doc = new XWPFDocument();
            foreach (var ClassesItem in nClassContents)
            {
                foreach (var MethodsItem in ClassesItem.Methods)
                {

                    XWPFParagraph p1 = doc.CreateParagraph();
                    p1.CreateRun().SetText("");
                    XWPFParagraph p2 = doc.CreateParagraph();
                    XWPFRun run1 = p2.CreateRun();
                    run1.SetText("接口名称:" + MethodsItem.Name);
                    run1.FontSize = 18;
                    run1.SetFontFamily("华文楷体", FontCharRange.None);

                    //确定行数，创建表格
                    var trow = 3;
                    trow = MethodsItem.ArgumentProperties == null ? trow + 2 : MethodsItem.ArgumentProperties.Count() + trow + 2;
                    trow = MethodsItem.ResultProperties == null ? trow + 2 : MethodsItem.ResultProperties.Count() + trow + 2;
                    if (MethodsItem.ArgumentProperties.Count() != 0)
                    {
                        foreach (var item in MethodsItem.ArgumentProperties)
                        {
                            if (item.Type == "List`1")
                            {
                                trow += item.SubProperties.ToList().Count;
                            }
                        }
                    }
                    if (MethodsItem.ResultProperties != null)
                    {
                        foreach (var item in MethodsItem.ResultProperties)
                        {
                            if (item.Type == "List`1")
                            {
                                trow += item.SubProperties.ToList().Count;
                            }
                        }
                    }
                    int curRow = 0;
                    var table = doc.CreateTable(trow, 5);
                    table.Width = 1000 * 5;
                    //设置列宽
                    table.SetColumnWidth(0, 1700);/* 设置列宽 */
                    table.SetColumnWidth(1, 600);/* 设置列宽 */
                    table.SetColumnWidth(2, 600);/* 设置列宽 */
                    table.SetColumnWidth(3, 400);/* 设置列宽 */
                    table.SetColumnWidth(4, 1700);/* 设置列宽 */



                    //table1
                    table.GetRow(curRow).MergeCells(0, 4);
                    table.GetRow(curRow).GetCell(0).SetText(MethodsItem.Name ?? string.Empty);
                    curRow++;
                    table.GetRow(curRow).MergeCells(1, 4);
                    table.GetRow(curRow).GetCell(0).SetText("请求方式");
                    table.GetRow(curRow).GetCell(1).SetText(MethodsItem.Method ?? string.Empty);
                    curRow++;
                    table.GetRow(curRow).MergeCells(1, 4);
                    table.GetRow(curRow).GetCell(0).SetText("请求链接");
                    table.GetRow(curRow).GetCell(1).SetText(MethodsItem.Uri ?? string.Empty);
                    curRow++;

                    //table2
                    if (MethodsItem.ArgumentProperties.Count() == 0)
                    {
                        table.GetRow(curRow).MergeCells(0, 4);
                        table.GetRow(curRow).GetCell(0).SetText("请求参数");
                        curRow++;
                        table.GetRow(curRow).MergeCells(0, 4);
                        table.GetRow(curRow).GetCell(0).SetText("空");
                        curRow++;
                    }
                    else
                    {
                        table.GetRow(curRow).MergeCells(0, 4);
                        table.GetRow(curRow).GetCell(0).SetText("请求参数");
                        curRow++;
                        table.GetRow(curRow).GetCell(0).SetText("参数名");
                        table.GetRow(curRow).MergeCells(1, 3);
                        table.GetRow(curRow).GetCell(1).SetText("参数类型");
                        table.GetRow(curRow).GetCell(2).SetText("说明");
                        curRow++;
                        foreach (var ArgumentPropertiesItem in MethodsItem.ArgumentProperties)
                        {
                            table.GetRow(curRow).GetCell(0).SetText(ArgumentPropertiesItem.Name ?? string.Empty);
                            table.GetRow(curRow).MergeCells(1, 3);
                            table.GetRow(curRow).GetCell(1).SetText(ArgumentPropertiesItem.Type ?? string.Empty);
                            table.GetRow(curRow).GetCell(2).SetText(ArgumentPropertiesItem.Description ?? string.Empty);
                            curRow++;
                        }
                    }


                    //table3
                    if (MethodsItem.ResultProperties.Count() == 0)
                    {
                        table.GetRow(curRow).MergeCells(0, 4);
                        table.GetRow(curRow).GetCell(0).SetText("返回数据结构");
                        curRow++;
                        table.GetRow(curRow).MergeCells(0, 4);
                        table.GetRow(curRow).GetCell(0).SetText("空");
                        curRow++;

                    }
                    else
                    {
                        table.GetRow(curRow).MergeCells(0, 4);
                        table.GetRow(curRow).GetCell(0).SetText("返回数据结构");
                        curRow++;
                        table.GetRow(curRow).GetCell(0).SetText("参数名");
                        table.GetRow(curRow).MergeCells(1, 3);
                        table.GetRow(curRow).GetCell(1).SetText("参数类型");
                        table.GetRow(curRow).GetCell(2).SetText("说明");
                        curRow++;

                        foreach (var ResultPropertiesItem in MethodsItem.ResultProperties)
                        {

                            if (ResultPropertiesItem.Type == "List`1")
                            {
                                int curRowIndex = curRow;
                                table.GetRow(curRow).GetCell(0).SetText(ResultPropertiesItem.Name ?? string.Empty);
                                table.GetRow(curRow).GetCell(1).SetText(ResultPropertiesItem.Type ?? string.Empty);
                                table.GetRow(curRow).GetCell(2).SetText("名称");
                                table.GetRow(curRow).GetCell(3).SetText("类型");
                                table.GetRow(curRow).GetCell(4).SetText(ResultPropertiesItem.Description ?? string.Empty);


                                curRow++;
                                foreach (var SubPropertiesItem in ResultPropertiesItem.SubProperties)
                                {
                                    table.GetRow(curRow).GetCell(2).SetText(SubPropertiesItem.Name ?? string.Empty);
                                    table.GetRow(curRow).GetCell(3).SetText(SubPropertiesItem.Type ?? string.Empty);
                                    curRow++;
                                }
                                //上下合并单元格
                                MYMergeCells(table, 0, 0, curRowIndex, curRow);
                                MYMergeCells(table, 1, 1, curRowIndex, curRow);
                            }
                            else
                            {
                                table.GetRow(curRow).GetCell(0).SetText(ResultPropertiesItem.Name ?? string.Empty);
                                table.GetRow(curRow).MergeCells(1, 3);
                                table.GetRow(curRow).GetCell(1).SetText(ResultPropertiesItem.Type ?? string.Empty);
                                table.GetRow(curRow).GetCell(2).SetText(ResultPropertiesItem.Description ?? string.Empty);
                                curRow++;
                            }
                        }
                    }
                }

            }
            doc.Write(_nDocumentInfo.FileStream);
            _nDocumentInfo.FileStream.Close();
            _nDocumentInfo.FileStream.Dispose();
            return _nDocumentInfo;

        }


        public static void MYMergeCells(XWPFTable table, int fromCol, int toCol, int fromRow, int toRow)
        {
            for (int rowIndex = fromRow; rowIndex < toRow; rowIndex++)
            {
                if (fromCol < toCol)
                {
                    table.GetRow(rowIndex).MergeCells(fromCol, toCol);
                }
                XWPFTableCell rowcell = table.GetRow(rowIndex).GetCell(fromCol);
                CT_Tc cttc = rowcell.GetCTTc();
                if (cttc.tcPr == null)
                {
                    cttc.AddNewTcPr();
                }
                if (rowIndex == fromRow)
                {
                    // The first merged cell is set with RESTART merge value  
                    rowcell.GetCTTc().tcPr.AddNewVMerge().val = ST_Merge.restart;
                }
                else
                {
                    // Cells which join (merge) the first one, are set with CONTINUE  
                    rowcell.GetCTTc().tcPr.AddNewVMerge().val = ST_Merge.@continue;
                }
            }

        }
    }
}
 